package com.xyy.saas.payment.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xyy.saas.payment.cores.api.PaymentOrderApi;
import com.xyy.saas.payment.cores.bo.OrderWithdrawalBo;
import com.xyy.saas.payment.cores.constants.CallbackConstants;
import com.xyy.saas.payment.cores.enums.*;
import com.xyy.saas.payment.cores.vo.OrderWithdrawVo;
import com.xyy.saas.payment.cores.vo.ResultVO;
import com.xyy.saas.payment.dao.mapper.WithdrawOrderMapper;
import com.xyy.saas.payment.dao.model.PayAccount;
import com.xyy.saas.payment.dao.model.WithdrawOrder;
import com.xyy.saas.payment.exception.PaymentException;
import com.xyy.saas.payment.pojo.bo.PlatformAccountBo;
import com.xyy.saas.payment.pojo.bo.WithdrawalRecordViewBo;
import com.xyy.saas.payment.pojo.vo.PlatformAccountWithdrawalVo;
import com.xyy.saas.payment.manage.cores.vo.ResultPageVo;
import com.xyy.saas.payment.pojo.vo.WithdrawalRecordsVo;
import com.xyy.saas.payment.service.PayAccountService;
import com.xyy.saas.payment.service.PlatformAccountsManage;
import com.xyy.saas.payment.service.WithdrawalService;
import com.xyy.saas.payment.util.DateUtil;
import com.xyy.saas.payment.util.LeafUtil;
import com.xyy.saas.payment.util.PageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangtianqi
 * @date 2020/11/2 15:32
 */
@Service
public class WithdrawalServiceImpl implements WithdrawalService {

    @Reference(version = "1.0.0")
    private PaymentOrderApi paymentOrderApi;

    @Autowired
    private WithdrawOrderMapper withdrawOrderMapper;

    @Autowired
    private PlatformAccountsManage platformAccountsManage;

    @Autowired
    private PayAccountService payAccountService;
    @Autowired
    private LeafUtil leafUtil;

    @Override
    public ResultPageVo accountWithdrawal(PlatformAccountWithdrawalVo platformAccountWithdrawalVo) {
        int amount = new BigDecimal(platformAccountWithdrawalVo.getAmount()).multiply(new BigDecimal(100)).intValue();
        PayAccount payAccount = payAccountService.getPayAccountByAccountId(platformAccountWithdrawalVo.getAccountId());

        OrderWithdrawVo orderWithdrawVo = new OrderWithdrawVo();
        orderWithdrawVo.setAmount(amount);
        orderWithdrawVo.setBusinessId(payAccount.getBusinessUserId());
        orderWithdrawVo.setDescription("平台户提现");
        orderWithdrawVo.setCallback(CallbackConstants.NO_NOTIFY);
        orderWithdrawVo.setBusinessType(com.xyy.saas.payment.manage.cores.enums.BusinessIdTypeEnum.getBusinessType(payAccount.getBusinessIdType()));
        orderWithdrawVo.setAccountChannelEnum(AccountChannelEnum.AGGREGATE);
        orderWithdrawVo.setBusinessIdType(payAccount.getBusinessIdType());
        // 后台管理系统的提现业务单号 加后缀 _backend
        orderWithdrawVo.setBusinessWithdrawNo(leafUtil.getSnowflakeId()+"_backend");
        ResultVO<OrderWithdrawalBo> resultVO = paymentOrderApi.withdraw(orderWithdrawVo, "");
        if(ResultCodeEnum.SUCCESS.getCode().equals(resultVO.getCode())) {
            return ResultPageVo.createSuccess();
        }else {
            return ResultPageVo.createFail("提现失败！" +resultVO.getMsg());
        }
    }

    @Override
    public ResultPageVo<List<WithdrawalRecordViewBo>> queryAccountWithdrawalRecords(WithdrawalRecordsVo withdrawalRecordsVo) throws PaymentException {
        int totalCount = withdrawOrderMapper.selectCountByUserIdAndCondition(withdrawalRecordsVo.getAccountId());
        List<WithdrawalRecordViewBo> resultDatas = new ArrayList<>();
        if(totalCount != 0) {
            PlatformAccountBo bo = platformAccountsManage.queryAccountByAccountId(withdrawalRecordsVo.getAccountId());
            List<WithdrawOrder> list = withdrawOrderMapper.selectByUserIdAndCondition(withdrawalRecordsVo.getAccountId(), PageUtil.getPageOffset(withdrawalRecordsVo.getPage(), withdrawalRecordsVo.getPageSize()), withdrawalRecordsVo.getPageSize());
            for(WithdrawOrder withdrawOrder : list) {
                WithdrawalRecordViewBo withdrawalRecordViewBo = convertWithdrawalRecordViewBo(withdrawOrder);
                withdrawalRecordViewBo.setAccountName(bo.getAccountName());
                resultDatas.add(withdrawalRecordViewBo);
            }
        }
        return ResultPageVo.createSuccess(withdrawalRecordsVo.getPage(), withdrawalRecordsVo.getPageSize(), Long.valueOf(totalCount), resultDatas);
    }

    private WithdrawalRecordViewBo convertWithdrawalRecordViewBo(WithdrawOrder withdrawOrder) {
        WithdrawalRecordViewBo withdrawalRecordViewBo = new WithdrawalRecordViewBo();
        withdrawalRecordViewBo.setAmount((new BigDecimal(withdrawOrder.getAmount()).divide(new BigDecimal(100)).toString()));
        if(WithdrawOrder.STATUS_CREATE.equals(withdrawOrder.getStatus())) {
            withdrawalRecordViewBo.setStatus("已创建");
        }
        if(WithdrawOrder.STATUS_IN.equals(withdrawOrder.getStatus())) {
            withdrawalRecordViewBo.setStatus("提现中");
        }
        if(WithdrawOrder.STATUS_FINISH.equals(withdrawOrder.getStatus())) {
            withdrawalRecordViewBo.setStatus("提现完成");
            withdrawalRecordViewBo.setSuccessDate(DateUtil.format(withdrawOrder.getSuccessTime(), DateUtil.DATEFORMATSECOND));
        }
        if(WithdrawOrder.STATUS_FAIL.equals(withdrawOrder.getStatus())) {
            withdrawalRecordViewBo.setStatus("提现失败");
        }
        withdrawalRecordViewBo.setWithdrawalDate(DateUtil.format(withdrawOrder.getCreateTime(), DateUtil.DATEFORMATSECOND));
        return withdrawalRecordViewBo;
    }


}
